<?php

class loginController extends AppController
{


    public function loginAction()
    {

        $account = (!empty($this->params['account'])) ? $this->params['account'] : "";
        $pwd = (!empty($this->params['pwd'])) ? $this->params['pwd'] : "";
        $validate = (!empty($this->params['valid'])) ? $this->params['valid'] : "";
        $nameFlag = (!empty($this->params['rmp'])) ? $this->params['rmp'] : "";

        $session_validate = !empty($_SESSION["validate"]) ? $_SESSION["validate"] : '';

        if (!empty($_SESSION["zp_company"]) && count($_SESSION["zp_company"]) > 0 && $_SESSION['zp_company']['islogin'] == true) {
            Tools::AlertTxt('index.php?md=company&cl=company&at=home', '');
            return;
        }

        $this->view->setVariable("account", $account);
        $this->view->render();
    }

    public function logincheckAction()
    {

        $account = (!empty($this->params['account'])) ? $this->params['account'] : "";
        $pwd = (!empty($this->params['pwd'])) ? $this->params['pwd'] : "";
        $validate = (!empty($this->params['valid'])) ? $this->params['valid'] : "";
        $session_validate = !empty($_SESSION['validate']) ?$_SESSION['validate'] : '';

        $ajax_result = array("status" => 0, "msg" => '');

        if (empty($account) || empty($pwd)) {
            $ajax_result["msg"] = '请输入帐号密码';
            // } else if (empty($validate)) {
            //     $ajax_result["msg"] = '请输入验证码';
            // } else if (strtolower($validate) != strtolower($session_validate)) {
            //     $ajax_result["msg"] = '验证码输入错误';
        } else {

            $rows = $this->model->loginCheck($account, $pwd);

            if (empty($rows) || $rows['isdeleted'] == 1) {
                $ajax_result["msg"] = '无此帐号';
            } else
                if (empty($rows["status"])) {
                    $ajax_result["msg"] = '请与管理者联系';
                } else

                    if ($rows['password'] == sha1($rows['salt'] . $pwd)) {
                        $_SESSION['zp_company']['company_code'] = $rows['code'];
                        $_SESSION['zp_company']['company_name'] = $rows['name'];
                        $_SESSION['zp_company']['islogin'] = true;
                        $_SESSION['login']['count'] = 0;
                        $_SESSION['zp_company']['zp_company_code'] = $rows['code'];
                        $_SESSION['zp_company']['time'] = date('Y-m-d H:i', time());




                        $list = Data::getAllAuthList('3');
                        $data = array();
                        foreach ($list as $k => $v) {
                            $data[]=$k;
                             if (isset($v['sub'])) {

                                foreach ($v['sub'] as $f => $value) {
                                    $data[]=$f;
                                }
                            }
                        }

                        $_SESSION['zp_company']['permission'] = join(" ",$data);
                        $ajax_result["status"] = '1';
                        $ajax_result["msg"] = '登录成功';
                        $ajax_result["url"] = 'index.php?md=company&cl=company&at=home';

                    } else {
                        $ajax_result["msg"] = '帐号或密码有误';
                    }
        }

        echo json_encode($ajax_result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    public function logoutAction()
    {
        if (isset($_SESSION['zp_company']) && !empty($_SESSION['zp_company'])) {
            unset($_SESSION['zp_company']);
        }
        $this->loginAction();
    }


}